package com.jarvis.dubbo;

import com.alibaba.dubbo.rpc.RpcException;
import com.jarvis.dubbo.pojo.ValidationModel;
import com.jarvis.dubbo.service.IValidationService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * TODO XXX
 *
 * @author jarvis
 * created in 2020/9/18
 */
public class  ValidationConsumer {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("spring-context-consumer.xml");
        IValidationService validationService = classPathXmlApplicationContext.getBean(IValidationService.class);
        System.out.println(validationService.insert(new ValidationModel("1", "2")));
        try{
            System.out.println(validationService.update(new ValidationModel(null, "123")));
        }catch (ConstraintViolationException e){
            Set<ConstraintViolation<?>> violations = e.getConstraintViolations(); // 可以拿到一个验证错误详细信息的集合
            System.out.println(String.format("更新错误信息: %s",violations.stream().map(mapper -> mapper.getMessage()).collect(Collectors.toList())));
        }
        try{
            System.out.println(validationService.insert(new ValidationModel()));
        }catch (ConstraintViolationException e){
            Set<ConstraintViolation<?>> violations = e.getConstraintViolations(); // 可以拿到一个验证错误详细信息的集合
            System.out.println(String.format("新增错误信息: %s",violations.stream().map(mapper -> mapper.getMessage()).collect(Collectors.toList())));
        }
    }
}
